package com.example.persistence;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.example.R;
import com.example.entity.Bookmark;
import com.example.entity.Const;
import com.example.entity.ItemList;
import com.example.util.FileUtil;
import com.example.util.StringUtil;

public class BaseActivity extends Activity implements Const {
	public static SimpleDateFormat imageFileNameFormat =  new SimpleDateFormat(IMAGE_FILE_PATTERN, Locale.KOREA);
	public MyApplication application;

	@Override
	public String getInternalRootPath() {
		return this.getFilesDir().getAbsolutePath();	
	}
	
	public String getBookmarkPath() {
		return getInternalRootPath() + "/Bookmark/";
	}
	
	public String getCapturePath() {
		return EXTERNAL_ROOT_PATH + "/Capture/"; 
	}
	
	public String getCaptureThumbnailPath() {
		return getCapturePath() + "Thumb/" ;
	}
	
	public String getBookmarkFileName() {
		return getBookmarkPath() + imageFileNameFormat.format(new Date()).toString() + ".png";
	}
	
	public String getCaptureFileName() {
		return getCapturePath() + imageFileNameFormat.format(new Date()).toString() + ".png";
	}
	
	public String getCaptureKey() {
		return imageFileNameFormat.format(new Date()).toString();
	}

	public void deleteLocalFile(String file) {
		String path = StringUtil.removePrefix(file, FileUtil.ANDROID_FILE_PREFIX);
		FileUtil.delete(path);
	}
	
	public String getLocalFileName(String file) {
		return StringUtil.removePrefix(file, FileUtil.ANDROID_FILE_PREFIX);
	}
	
	public File getLocalFile(String file) {
		return new File(StringUtil.removePrefix(file, FileUtil.ANDROID_FILE_PREFIX));
	}
	
	public void clearBookmarkList(ItemList<Bookmark> list) {
		FileUtil.deleteFiles(getBookmarkPath());
		FileUtil.delete(BOOKMARK_FILE);
		if (list != null)
			list.clear();
	}
	
	public void clearCaptureList(List<String> list) {
		FileUtil.deleteFiles(getCapturePath());
		if (list != null)
			list.clear();
	}
	
	public void clearAllData() {
		FileUtil.deleteFile(this, BOOKMARK_FILE);
        clearBookmarkList(null);
        clearCaptureList(null);	
	}
	
	public void clearAllData(ItemList<Bookmark> bookmarkList) {
		FileUtil.deleteFile(this, BOOKMARK_FILE);
        clearBookmarkList(bookmarkList);
        clearCaptureList(null);
	}
	
	public void alert(String message) {
		new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle(R.string.app_name)
			.setMessage(message)
			.setPositiveButton(android.R.string.ok, null)
			.create().show();
	}
	
	public void alert(String message, OnClickListener Positive, OnClickListener Negative) {
		new AlertDialog.Builder(this)
			.setIcon(android.R.drawable.ic_dialog_alert)
			.setTitle(R.string.app_name)
			.setMessage(message)
			.setPositiveButton(android.R.string.ok, Positive)
			.setNegativeButton(android.R.string.cancel, Negative)
			.create().show();
	}
	
	public void notifyCaptureFile() {
		sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse(FileUtil.ANDROID_FILE_PREFIX + getCapturePath())));
	}
	
	  /**
	   * 픽셀단위를 현재 디스플레이 화면에 비례한 크기로 반환합니다.
	   * 
	   * @param pixel 픽셀
	   * @return 변환된 값 (DP)
	   */
	public int getDPFromPixel(int pixel) {
		float scale = getResources().getDisplayMetrics().density;
		return (int) (pixel / DEFAULT_HDIP_DENSITY_SCALE * scale);
	}
	  
	  /**
	   * 현재 디스플레이 화면에 비례한 DP단위를 픽셀 크기로 반환합니다.
	   * 
	   * @param  DP 픽셀
	   * @return 변환된 값 (pixel)
	   */
	  public int getPixelFromDP(int DP) {
	    float scale = getResources().getDisplayMetrics().density;
	    return (int)(DP / scale * DEFAULT_HDIP_DENSITY_SCALE);
	  }
	  
	  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("BaseActivity onCreate", "[" + this + "]");
		application = (MyApplication) getApplication();
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onStart() {
		Log.i("BaseActivity onStart", "[" + this + "]");
		
		super.onStart();
	}
	
	@Override
	protected void onResume() {
		Log.i("BaseActivity onResume", "[" + this + "]");
		
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		Log.i("BaseActivity onPause", "[" + this + "]");
		
		super.onPause();
	}
	
	@Override
	protected void onStop() {
		Log.i("BaseActivity onStop", "[" + this + "]");
		
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		Log.i("BaseActivity onDestroy", "[" + this + "]");
		application.saveData();
		
		super.onDestroy();
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		Log.i("BaseActivity onWindowFocusChanged", "[" + this + "] hasFocus=" + hasFocus);
		
		super.onWindowFocusChanged(hasFocus);
	}
}
